//
//  ViewController.m
//  post
//
//  Created by Hyo Jung Lee on 11. 12. 30..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import "JSONKit.h"
#import "FMDatabase.h"

@implementation ViewController

@synthesize data = data_;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    
#if 0
    {
        //    NSString *URLString = @"http://localhost:8888/talk2/member/join.php?ID=ma050&PWD=aa1122&NAME=남자50호&SEX=M&AGE=34";
        //    NSString *URLString = @"http://localhost:8888/talk2/member/join.php?ID=fa10&PWD=aa1122&NAME=여자10호&SEX=F&AGE=34&REGION=12";
        NSString * URLString = @"http://kih.woobi.co.kr/post/joinCheck.php?ID=admin";
        NSString *encodeURLString = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        NSURL   *URL = [NSURL URLWithString:encodeURLString];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
        NSURLConnection *connection = [NSURLConnection connectionWithRequest:request 
                                                                    delegate:self];
        [connection start];        
    }
#endif
    
    
    
    
    
#if 0
    {
        FMDatabase* db = [FMDatabase databaseWithPath:@"success.sqlite"];
        if (![db open]) {
            NSLog(@"Could not open db.");
            return;
        }
        
        
        [db executeUpdate:@"CREATE TABLE quiz (no INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , title VARCHAR, question VARCHAR, answer VARCHAR, level VARCHAR)"];
        [db beginTransaction];
        int count = 0;
        while (count++ < 3) {
            [db executeUpdate:@"insert into quiz (title, question, answer, level) values (?, ?, ?, ?)" ,
             @"hi", // look!  I put in a ', and I'm not escaping it!
             @"dadfasdfasdf",
             @"sdfasfasdf",
             @"1"];
        }
        [db commit];
        
        FMResultSet *rs = [db executeQuery:@"select * from quiz where title = ?", @"hi"];
        
        NSLog(@"%@, %@, %@, %@, %@", [rs columnNameForIndex:0], 
              [rs columnNameForIndex:1],
              [rs columnNameForIndex:2],
              [rs columnNameForIndex:3],
              [rs columnNameForIndex:4]);

        while ([rs next]) {
            // just print out what we've got in a number of formats.
            NSLog(@"%d %@ %@ %@ %@",
                  [rs intForColumn:@"no"],
                  [rs stringForColumn:@"title"],
                  [rs stringForColumn:@"question"],
                  [rs stringForColumn:@"answer"],
                  [rs stringForColumn:@"level"]);
            
            NSLog(@"%d %@ %@ %@ %@",
                  [rs intForColumnIndex:0],
                  [rs stringForColumnIndex:1],
                  [rs stringForColumnIndex:2],
                  [rs stringForColumnIndex:3],
                  [rs stringForColumnIndex:4]);
        }
        // close the result set.
        // it'll also close when it's dealloc'd, but we're closing the database before
        // the autorelease pool closes, so sqlite will complain about it.
        [rs close];
    }
#endif
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//}
//
//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//}
//
//- (void)viewWillDisappear:(BOOL)animated
//{
//	[super viewWillDisappear:animated];
//}
//
//- (void)viewDidDisappear:(BOOL)animated
//{
//	[super viewDidDisappear:animated];
//}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    // Return YES for supported orientations
//    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
//    } else {
//        return YES;
//    }
//}



#pragma -
#pragma NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection 
  didFailWithError:(NSError *)error
{
    NSAssert(NO, @"%@", [error localizedDescription]);
}

- (void)connection:(NSURLConnection *)connection 
didReceiveResponse:(NSURLResponse *)response
{
    self.data = [[NSMutableData alloc] initWithCapacity:1024];
}

- (void)connection:(NSURLConnection *)connection 
    didReceiveData:(NSData *)data
{
    [self.data appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//    NSString *responseDataString  = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];
//    NSLog(@"1. responseDataString [%@]", responseDataString);
//    
//    responseDataString = [responseDataString stringByReplacingOccurrencesOfString:@"+" withString:@" "];
//    NSLog(@"1-1. responseDataString [%@]", responseDataString);
//    
//    responseDataString = [responseDataString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    NSLog(@"2. responseDataString [%@]", responseDataString);
//
//    
//    NSDictionary *obj = [[JSONDecoder decoder] objectWithData:self.data];
//    NSDictionary *obj = [responseDataString objectFromJSONString];
    
    
    NSDictionary *obj = [[JSONDecoder decoder] objectWithEncodedData:self.data];
    
    [[[[UIAlertView alloc] initWithTitle:@"response" 
                                 message:[obj objectForKey:@"MESSAGE"]
                                delegate:nil 
                       cancelButtonTitle:@"OK" 
                       otherButtonTitles:nil, nil] autorelease] show];
    
    NSLog(@"obj [%@]", obj);
}

@end
